package br.folhapgto.dao;

import br.folhapgto.dao.banco.Conexao;
import br.folhapgto.dao.idao.IHistoricoPgtoDAO;
import br.folhapgto.model.HistoricoPgto;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class HistoricoPgtoDAO implements IHistoricoPgtoDAO
{
	private static HistoricoPgtoDAO instanciaUnica = new HistoricoPgtoDAO();

    public static HistoricoPgtoDAO getInstanciaUnica() 
    {
        return instanciaUnica;
    }

    // Construtor
    public HistoricoPgtoDAO()
    {
    	
    }
    
    // M�todos CRUD
    public HistoricoPgto carregaDados(ResultSet rs) throws SQLException
    {
    	HistoricoPgto histPgto = new HistoricoPgto();
        
    	histPgto.setIdHistPgto(rs.getInt("idHistPgto"));
    	histPgto.setIdEmpregado(rs.getInt("idEmpregado"));
    	histPgto.setDataPgto(rs.getDate("dataPgto"));
    	histPgto.setValor(rs.getFloat("valor"));
                
        return histPgto;
    }
    
    public Integer gravar(HistoricoPgto histPgto)
    {

        int retornoBanco = 0;
        Connection conn = Conexao.getInstanciaUnica().criaConexao();

        if (conn != null) {
            PreparedStatement pstmt = null;
            try {
                pstmt = conn.prepareStatement(
                        "INSERT INTO TB_HIST_PGTO (idEmpregado, " + 
                        "dataPgto, " +
                        "valor)" +
                        " VALUES (?, ?, ?)");
                pstmt.setInt(1, histPgto.getIdEmpregado());
                pstmt.setDate(2, (java.sql.Date)histPgto.getDataPgto());
                pstmt.setFloat(3, histPgto.getValor());

				retornoBanco = pstmt.executeUpdate();
            } catch (SQLException e) {
                System.out.println("Inclusao Falhou!!!\n" + e.getMessage());
            } finally {
                try {
                    if (pstmt != null) {
                        pstmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }

        return retornoBanco;
    }
    
	public List<HistoricoPgto> lerTodos()
    {
    	Statement stmt = null;
        List<HistoricoPgto> histPgtos = new ArrayList<HistoricoPgto>();
        
        Connection conn = Conexao.getInstanciaUnica().criaConexao();
        
        if (conn != null) {
            try {
                stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(
                        "SELECT * FROM TB_HIST_PGTO");
                rs.last();
                rs.beforeFirst();
                while (rs.next()) {
                	histPgtos.add(carregaDados(rs));
                }
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return histPgtos;
    }

	public HistoricoPgto ler(Integer idHistPgto)
	{
		Statement stmt = null;
		HistoricoPgto histPgto = null;
        Connection conn = Conexao.getInstanciaUnica().criaConexao();
        
        if (conn != null) {
            try {
                stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(
                        "SELECT * FROM TB_HIST_PGTO WHERE idHistPgto = " +
                        idHistPgto);
                if (rs.next()) {
                	histPgto = carregaDados(rs);
                } else {
                	histPgto = null;
                }
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return histPgto;
	}
	
	public Integer remover(Integer idHistPgto)
	{
		int n = 0;
        Statement stmt = null;
        Connection conn = Conexao.getInstanciaUnica().criaConexao();

        if (conn != null) {
            try {
                stmt = conn.createStatement();
                n = stmt.executeUpdate(
                        "DELETE FROM TB_HIST_PGTO WHERE idHistPgto = " +
                        idHistPgto);
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return n;
	}
	
	public Integer alterar(HistoricoPgto histPgto)
	{
		int n = 0;
        Connection conn = Conexao.getInstanciaUnica().criaConexao();

        if (conn != null) {
            PreparedStatement pstmt = null;
            try {
                pstmt = conn.prepareStatement(
                        "UPDATE TB_HIST_PGTO SET " +
                        "idEmpregado = ?, " + 
                        "dataPgto = ?, " +
                        "valor = ? " +
                        "WHERE idHistPgto = ?");
                
                pstmt.setInt(1, histPgto.getIdEmpregado());
                pstmt.setDate(2, (java.sql.Date)histPgto.getDataPgto());
                pstmt.setFloat(3, histPgto.getValor());
				pstmt.setInt(4, histPgto.getIdHistPgto());
				
                n = pstmt.executeUpdate();
                conn.close();
                
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (pstmt != null) {
                        pstmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return n;
	}
}
